﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
public partial class Tools_Game : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostBack)
      {
          Bind();
      }
    }

    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from Game";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        int Gameid = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select src from Game where Gameid=" + Gameid + "";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        Process proc = new Process();
        proc.StartInfo.FileName = dr["src"].ToString();
        proc.Start();
        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select dutyid from userinfo where username='" + Session["UserName"].ToString() + "'";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        int dutyid = Convert.ToInt32(dr.GetValue(0));
        if (dutyid != 7)
        {
            this.Label1.Text = "对不起,只有管理员才能操作.如有需要请与管理员联系！";
        }
        else
        {
            this.Mypanel.Visible = true;
        }
    }
    protected void Button2_Click1(object sender, EventArgs e)
    {

        string src = "";
        if (FileUpload1.PostedFile.ContentLength == 0)
        {
            Response.Write("<script>alert('请上传文件！')</script>");
            return;

        }
        else if (this.TextBox1.Text.Trim() == "")
        {
            Response.Write("<script>alert('工具名不能为空')</script>");
            return;
        }
        else
        {

            string strFilePath = FileUpload1.PostedFile.FileName;//E:\\OA_Test\\Images\\sel2.gif

            FileInfo fl = new FileInfo(strFilePath);
            string ServerPath = Server.MapPath("~");
            string Ext = fl.Name;
            src = Ext;
            string strSeraPath = ServerPath + "\\ToolGame\\" + src;
            FileUpload1.PostedFile.SaveAs(strSeraPath);

            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "insert Game values('" + TextBox1.Text.Trim() + "','" + strSeraPath + "')";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            con.Close();
            Bind();
            this.Label1.Text = "上传成功";
        }


    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int i = Check();
        if (i != 7)
        {
            this.Label1.Text = "对不起,只有管理员才能删除";
        }
        else
        {
            int toolid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con1.Open();
            string sql1 = "select src from tool where toolid=" + toolid + "";
            SqlCommand com1 = new SqlCommand(sql1, con1);
            SqlDataReader dr1 = com1.ExecuteReader();
            dr1.Read();
            string url = dr1["src"].ToString();
            if (File.Exists(url))
            {
                File.Delete(url);
            }
            con1.Close();

            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "delete from tool where toolid=" + toolid + "";
            SqlCommand com = new SqlCommand(sql, con);
            com.ExecuteNonQuery();
            this.Label1.Text = "删除成功";
            con.Close();
            Bind();
        }
    }
    public int Check()
    {
        int i = 0;
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select dutyid from userinfo where username='" + Session["UserName"].ToString() + "'";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        int dutyid = Convert.ToInt32(dr.GetValue(0));
        i = dutyid;
        return i;
    }
}
